import {observable, action, computed} from 'mobx';

class AppleStore {
    @observable appleList = [];
    appNo = 0;
    @action.bound addApple() {
        this.appNo++;
        this.appleList.push({
            appNo: this.appNo,
            weight: Math.floor(Math.random() * 100) + 150,
            isEat: false
        });
    }
    @action.bound eatApple(appNo){
        const apple = this.appleList.find(apple => apple.appNo === appNo);
        apple.isEat = true;
    }
    @computed get appleFilter(){
        return this.appleList.filter(apple => !apple.isEat);
    }
    @computed get currentApples(){
        const list = this.appleList.filter(apple => !apple.isEat);
        const total = list.reduce((total, apple) => {
            return total + apple.weight;
        },0);
        return {num: list.length, weight: total}
    }
    @computed get eatApples(){
        const list = this.appleList.filter(apple => apple.isEat);
        const total = list.reduce((total, apple) => {
            return total + apple.weight;
        },0);
        return {num: list.length, weight: total}
    }
}

const apple = new AppleStore();

export default apple;
